﻿
using System.Collections.Generic;
using System.Reflection;
using Nest;
using NHibernate.Search.ElasticSearch.Attributes;
using NHibernate.Search.ElasticSearch.Cfg;
namespace NHibernate.Search.ElasticSearch.Mapping
{
    public class IndexedClass
    {
        private NHibernateSearchConfiguration cfg;
        private System.Type type;
        public string Type
        {
            get { return type.Name; }
        }
        public string Index
        {
            get { return cfg.Index; }
        }
        internal IndexedClass(NHibernateSearchConfiguration cfg, System.Type type)
        {
            this.cfg = cfg;
            this.type = type;
            InitializeFields();
            // TODO 判断是否存在Type，如果存在，则更新，否则创建。

        }
        private void InitializeFields()
        {

            // DeleteMappingRequest deleteMappingRequest = new DeleteMappingRequest(
            //new IndexNameMarker() { Name = "inovout" }, new TypeNameMarker() { Name = type.Name });
            // cfg.GetElasticClient().DeleteMapping(deleteMappingRequest);

            //PutMappingRequest putMappingRequest = new PutMappingRequest(
            //        new IndexNameMarker() { Name = cfg.Index }, new TypeNameMarker() { Name = type.Name });
            ////RootObjectMapping rootObjectMapping = new RootObjectMapping();
            //Dictionary<PropertyNameMarker, IElasticType> properties = new Dictionary<PropertyNameMarker, IElasticType>();
            //返回当前类型的所有公共成员数组  
            //MemberInfo[] Members = type.GetMembers();
            //遍历数组，迭代每个成员信息  
            //foreach (MemberInfo memberInfo in Members)
            //{
            //    if (memberInfo.IsDefined(typeof(FieldAttribute)))
            //    {
            //        //properties.Add(memberInfo.Name, new StringMapping() { Analyzer = "default", Store = false });
            //    }
            //}
            //rootObjectMapping.Name = type.Name;
            //rootObjectMapping.Properties = properties;
            //putMappingRequest.Mapping = rootObjectMapping;
            //cfg.GetElasticClient().Map(putMappingRequest);

            //var myJson = new { name = "qw", QuanPinName = "qianwe" };
            //elasticClient.IndexAsync(myJson);

        }
    }
}
